package com.example.administrator.u17.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.QiangLiTuiJianMoreAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.model.QiangLiTuiJianMoreComics;
import com.example.administrator.u17.model.QiangLiTuiJianMoreRootBean;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class MoreActivity extends AppCompatActivity implements PullToRefreshBase.OnRefreshListener2{

    private PullToRefreshListView listView;
    private List<QiangLiTuiJianMoreComics> list;
    private QiangLiTuiJianMoreAdapter adapter;
    private TextView tvSpinner,tvTitle;
    private ImageView ivBack;
    private String argValue;
    private String argName;
    private int pages=1,con=2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_more);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(this);

    }

    private void setData() {
        Intent intent=getIntent();
        if (intent!=null){
            tvTitle.setText(intent.getStringExtra("title"));
            argValue = intent.getStringExtra("argValue");
            argName = intent.getStringExtra("argName");
        }
        loadData(argValue,argName,pages,2);
        listView.setAdapter(adapter);
    }

    private void initData() {
        list=new ArrayList<>();
        adapter=new QiangLiTuiJianMoreAdapter(this,list);
    }

    private void initView() {
        listView= (PullToRefreshListView) findViewById(R.id.listview_activity_more);
        tvSpinner=(TextView)findViewById(R.id.tv_activity_more_sipnner);
        ivBack=(ImageView)findViewById(R.id.iv_activity_more_back);
        tvTitle= (TextView) findViewById(R.id.tv_activity_more_title);
        registerForContextMenu(tvSpinner);
    }

    private void loadData(String value,String name,int page,int argCon){
        String path="http://app.u17.com/v3/appV3_1/android/phone/list/commonComicList?argValue="+value+"&argName="+name+"&argCon="+argCon+"&page="+page+"&v=3220102&key=63b17b78a8c7c6f07151a4f780904a82cdf7a0334d3a2f55ca6b1d7a053a9ebf4791dc2e10a65126385d9520a2db78fcfb40516d03a2e041b6a48e0afc6a1b59f947acef6624cd656fc00834bd18ff2b46b3d2de13c0e0ca97af712c2180766a1b937c9c926a9ed2e5e699350d908845a31f8dd2c4e19e80d6c2feea07d9c7f69005d7604390e58c68aa88fc920653831917fa676698a758%253A%253A%253Aopen&model=vivo+V3Max+A&come_from=openqq&android_id=7ca5c47776243605";
        final RequestParams params=new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                Gson g=new Gson();
                QiangLiTuiJianMoreRootBean bean=g.fromJson(s,QiangLiTuiJianMoreRootBean.class);
                if (pages==1){
                    list.clear();
                }
                pages++;
                list.addAll(bean.data.returnData.comics);
                if (bean.data.returnData.spinnerList.size()!=0){
                    tvSpinner.setVisibility(View.VISIBLE);
                }
                listView.onRefreshComplete();
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                Toast.makeText(MoreActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        loadData(argValue,argName,1,con);
        pages=1;
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        loadData(argValue,argName,pages,con);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu_update,menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.click:
                con=1;
                pages=1;
                list.clear();
                loadData(argValue,argName,pages,con);
                tvSpinner.setText("点击");
                break;
            case R.id.update:
                con=2;
                pages=1;
                list.clear();
                loadData(argValue,argName,pages,con);
                tvSpinner.setText("更新");
                break;
            case R.id.cang:
                con=3;
                pages=1;
                list.clear();
                loadData(argValue,argName,pages,con);
                tvSpinner.setText("收藏");
                break;
        }
        return true;
    }
}
